var Koa = require('koa');
var app = new Koa();
var Router = require('koa-router');
var router = new Router();
var koaBody = require('koa-body')();

router.get('/', function (ctx, next){
  ctx.body = 'hello koa!'
})

router.get('/api', function (ctx, next){
  ctx.body = 'test data'
})

router.get('/api/1', function (ctx, next){
  ctx.body = 'test data 1'
})

router.get('/api/2', function (ctx, next){
  ctx.body = {
    a: 1,
    b: '123'
  }
})
router.get('/api/homeDate', function (ctx, next){
  ctx.body = [
    {img: 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2129473072,572477714&fm=200&gp=0.jpg',
      link: 'http://www.baidu.com'
    },
    {img: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=615626109,85008197&fm=27&gp=0.jpg',
      link: 'http://www.baidu.com'
    },
    {img: 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=492336717,911941398&fm=27&gp=0.jpg',
      link: 'http://www.baidu.com'
    },
    {img: 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1873196274,3868187714&fm=200&gp=0.jpg',
      link: 'http://www.baidu.com'
    },
    {img: 'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2461994905,2925388020&fm=27&gp=0.jpg',
      link: 'http://www.baidu.com'
    },
    {img: 'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1708967154,2237615495&fm=27&gp=0.jpg',
      link: 'http://www.baidu.com'
    },
  ]
})

router.get('/api/homelist/:city/:page', function (ctx, next){
  console.log(ctx.params)
  const page = parseInt(ctx.params.page)
  if(page >= 4) {
    ctx.body = {
    hasMore: false,
    data: [
    {
      img: 'http://p0.meituan.net/deal/d1cdf7c56a7ade0b94600bdae4000993516194.jpg%40246w_184h_1e_1c_1l%7Cwatermark%3D1%26%26r%3D1%26p%3D9%26x%3D2%26y%3D2%26relative%3D1%26o%3D20',
      titile: '西树泡芙',
      subTitle: '仅售45元！最高价值58元的泡芙4个装',
      price: '45',
      distence: '120m',
      number: '389',
      id: 1
    },
     {
      img: 'http://p1.meituan.net/deal/959130088a4d086e2ea66c0d87897a621913805.jpg%40246w_184h_1e_1c_1l%7Cwatermark%3D1%26%26r%3D1%26p%3D9%26x%3D2%26y%3D2%26relative%3D1%26o%3D20',
      titile: '吴记老锅底麻辣火锅',
      subTitle: '仅售0.9元！价值58元的鸳鸯锅底（锅底内鸭血，豆腐免费续加）1份，提供免费WiFi。',
      price: '120',
      distence: '200m',
      number: '455',
      id: 2
    },
     {
      img: 'http://p0.meituan.net/deal/b1c807eb2c4fbd9e63f47c8cd59fb74f1567291.jpg%40246w_184h_1e_1c_1l%7Cwatermark%3D1%26%26r%3D1%26p%3D9%26x%3D2%26y%3D2%26relative%3D1%26o%3D20',
      titile: '韩膳宫料理',
      subTitle: '仅售199元！最高价值390元的<全新>精美套餐，建议3-4人使用，提供免费WiFi。',
      price: '562',
      distence: '1000m',
      number: '45',
      id: 3
    },
     {
      img: 'http://p1.meituan.net/deal/56cb0d4f78d4c7ed46a18c7bea943a57628805.jpg%40246w_184h_1e_1c_1l%7Cwatermark%3D1%26%26r%3D1%26p%3D9%26x%3D2%26y%3D2%26relative%3D1%26o%3D20',
      titile: '聪少甜品',
      subTitle: ' 仅售48元！最高价值64元的甜品套餐5选2，建议1-2人使用，提供免费WiFi。',
      price: '50',
      distence: '460m',
      number: '500',
      id: 4
    },
     {
      img: 'http://p1.meituan.net/deal/5c1c6264f24522de81fc0a9cbf0290df166832.jpg%40246w_184h_1e_1c_1l%7Cwatermark%3D1%26%26r%3D1%26p%3D9%26x%3D2%26y%3D2%26relative%3D1%26o%3D20',
      titile: '龙虾风暴',
      subTitle: ' 仅售258元！最高价值296元的双人精品套餐，提供免费WiFi。',
      price: '258',
      distence: '120m',
      number: '45',
      id: 5
    },
  ]
  }
  } else {
      ctx.body = {
    hasMore: true,
    data: [
    {
      img: 'http://p0.meituan.net/deal/d1cdf7c56a7ade0b94600bdae4000993516194.jpg%40246w_184h_1e_1c_1l%7Cwatermark%3D1%26%26r%3D1%26p%3D9%26x%3D2%26y%3D2%26relative%3D1%26o%3D20',
      titile: '西树泡芙',
      subTitle: '仅售45元！最高价值58元的泡芙4个装',
      price: '45',
      distence: '120m',
      number: '389',
      id: 1
    },
     {
      img: 'http://p1.meituan.net/deal/959130088a4d086e2ea66c0d87897a621913805.jpg%40246w_184h_1e_1c_1l%7Cwatermark%3D1%26%26r%3D1%26p%3D9%26x%3D2%26y%3D2%26relative%3D1%26o%3D20',
      titile: '吴记老锅底麻辣火锅',
      subTitle: '仅售0.9元！价值58元的鸳鸯锅底（锅底内鸭血，豆腐免费续加）1份，提供免费WiFi。',
      price: '120',
      distence: '200m',
      number: '455',
      id: 2
    },
     {
      img: 'http://p0.meituan.net/deal/b1c807eb2c4fbd9e63f47c8cd59fb74f1567291.jpg%40246w_184h_1e_1c_1l%7Cwatermark%3D1%26%26r%3D1%26p%3D9%26x%3D2%26y%3D2%26relative%3D1%26o%3D20',
      titile: '韩膳宫料理',
      subTitle: '仅售199元！最高价值390元的<全新>精美套餐，建议3-4人使用，提供免费WiFi。',
      price: '562',
      distence: '1000m',
      number: '45',
      id: 3
    },
     {
      img: 'http://p1.meituan.net/deal/56cb0d4f78d4c7ed46a18c7bea943a57628805.jpg%40246w_184h_1e_1c_1l%7Cwatermark%3D1%26%26r%3D1%26p%3D9%26x%3D2%26y%3D2%26relative%3D1%26o%3D20',
      titile: '聪少甜品',
      subTitle: ' 仅售48元！最高价值64元的甜品套餐5选2，建议1-2人使用，提供免费WiFi。',
      price: '50',
      distence: '460m',
      number: '500',
      id: 4
    },
     {
      img: 'http://p1.meituan.net/deal/5c1c6264f24522de81fc0a9cbf0290df166832.jpg%40246w_184h_1e_1c_1l%7Cwatermark%3D1%26%26r%3D1%26p%3D9%26x%3D2%26y%3D2%26relative%3D1%26o%3D20',
      titile: '龙虾风暴',
      subTitle: ' 仅售258元！最高价值296元的双人精品套餐，提供免费WiFi。',
      price: '258',
      distence: '120m',
      number: '45',
      id: 5
    },
  ]
  }
  }
})

router.get('/api/detail/info/:id',function(ctx, next) {
  ctx.body={
    desc: '营业时间 11:00 - 21:00 <br> 电话订购 11:00 - 21:00 <br> 网络订购 11:00 - 21:00',
    img: 'http://p1.meituan.net/deal/959130088a4d086e2ea66c0d87897a621913805.jpg%40246w_184h_1e_1c_1l%7Cwatermark%3D1%26%26r%3D1%26p%3D9%26x%3D2%26y%3D2%26relative%3D1%26o%3D20',
    price: '120',
    start: 4,
    subTitle: '重庆 & 四川 麻辣火锅',
    title: '天下第一锅'
  }
})

router.get('/api/detail/comment/:page/:id',function(ctx, next) {
  ctx.body=[
    {
      user: '13530375996',
      star: 5,
      desc: '非常好吃，下次再来'
    },
    {
      user: '13530375997',
      star: 4,
      desc: '羊肉最好吃'
    },
    {
      user: '13530375998',
      star: 3,
      desc: '分量太小啦，但是味道不错'
    },
    {
      user: '13530375999',
      star: 5,
      desc: '一日既往的好吃'
    },
    {
      user: '13530376008',
      star: 5,
      desc: '好好吃哦'
    }
  ]
})

router.get('/api/orderlist/:username', function(ctx, next) {
  ctx.body={
    data: [
      {
        count: 3,
        img: 'http://p0.meituan.net/deal/d1cdf7c56a7ade0b94600bdae4000993516194.jpg%40246w_184h_1e_1c_1l%7Cwatermark%3D1%26%26r%3D1%26p%3D9%26x%3D2%26y%3D2%26relative%3D1%26o%3D20',
        price: "25",
        title: '西树泡芙',
        commentState: 0,
        id: 123
      },
      {
        count: 2,
        img: 'http://p1.meituan.net/deal/56cb0d4f78d4c7ed46a18c7bea943a57628805.jpg%40246w_184h_1e_1c_1l%7Cwatermark%3D1%26%26r%3D1%26p%3D9%26x%3D2%26y%3D2%26relative%3D1%26o%3D20',
        price: "45",
        title: '聪少甜品',
        commentState: 0,
        id: 456
      },
      {
        count: 1,
        img: 'http://p1.meituan.net/deal/5c1c6264f24522de81fc0a9cbf0290df166832.jpg%40246w_184h_1e_1c_1l%7Cwatermark%3D1%26%26r%3D1%26p%3D9%26x%3D2%26y%3D2%26relative%3D1%26o%3D20',
        price: "157",
        title: '龙虾风暴',
        commentState: 2,
        id: 789
      }
    ]
  }
})


router.post('/api/post', koaBody , function (ctx,next) {
  console.log(ctx.request.body)
  ctx.body = JSON.stringify(ctx.request.body)
})

router.post('/api/submitComent', koaBody , function (ctx,next) {
  console.log(ctx.request.body)
  ctx.body = JSON.stringify(ctx.request.body)
})

app.use(router.routes())
    .use(router.allowedMethods())


app.listen(3000, function(err) {
  if(err){
    console.log(err)
  }
  console.log('ok!!!')
})